/**
 * 
 */
package eu.digitalidea.android.tutorial;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;

/**
 * @author Ahadu
 *
 */
public class Cornboyz extends View {


	private Context c;

	private Bitmap flowerWhole;
	private Bitmap flowerNoPetalOne;
	private Bitmap petalOne;
	
	int x, y;
	private Paint paint;
	private Paint pBlue;
	private Paint pGrid;
	private Paint pText;
	
	private boolean touched;

	private Paint pPaint;
	/**
	 * @param context
	 */
	public Cornboyz(Context context) {
		super(context);
		c = context;
		//flowerWhole = BitmapFactory.decodeResource(getResources(), R.drawable.flower_whole);
		flowerNoPetalOne = BitmapFactory.decodeResource(getResources(), R.drawable.flower_no_petal_1);
		petalOne = BitmapFactory.decodeResource(getResources(), R.drawable.petal_1);
		x = 0;
		y = 32;
		
		paint = new Paint();
		pBlue = new Paint();
		pGrid = new Paint();
		pPaint = new Paint();
		pText = new Paint();
		
		pBlue.setStyle(Paint.Style.FILL);
		pBlue.setColor(Color.GREEN);
		
		pGrid.setColor(Color.LTGRAY);
		
		pPaint.setColor(Color.RED);
		pPaint.setStrokeWidth(3);

		pText.setColor(Color.BLACK);
		pText.setTextSize(12);
		
		touched = false;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		// set background
		Rect cBk = new Rect();
		cBk.set(0, 0, canvas.getWidth(), canvas.getHeight());
//		y = canvas.getWidth()/10;
		// don't create Paint objs in onDraw!!!
		
		canvas.drawRect(cBk, pBlue);
		
		drawFlower(canvas.getWidth()/10,canvas.getWidth()/10, canvas);
		drawPetal(115, y+12, canvas);
		drawGrid(canvas);
		
		canvas.drawPoint(115, y+12, pPaint);
		canvas.drawText("("+115+", "+ (y+12)+")", 115, y-10, pText);
		if(touched){
			y--;
		}
		
//		
//		y--;
//		
//		if (y < -100){
//			y = canvas.getWidth()/10;
//		}else if ( y == 32){
//			for (int i = 0; i < 10000000; i++){
//				System.out.println("I will not write code in Java-Android while I have to work on C#-Visual Studio");
//			}
//		}
		
		invalidate();
		
				
	}

	

	private void drawPetal(int x2, int y2, Canvas canvas) {
		canvas.drawBitmap(petalOne, x2, y2, paint);
	}

	private void drawFlower(int x2, int y2, Canvas canvas) {
		canvas.drawBitmap(flowerNoPetalOne, x2, y2, paint);
	}
	
	private void drawGrid(Canvas canvas){
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		int offset = width / 10;
//		float[] pts = new float[350]; 
		float[] pts = {0,0,0, height, 
					   offset,0,offset,height,
					   2*offset,0,2*offset,height,
					   3*offset,0,3*offset,height, 
					   4*offset,0,4*offset,height,
					   5*offset,0,5*offset,height,
					   6*offset,0,6*offset,height,
					   7*offset,0,7*offset,height,
					   8*offset,0,8*offset,height,
					   9*offset,0,9*offset,height,
					   10*offset,0,10*offset,height,
					   0,0,width,0,
					   0,offset,width,offset,
					   0,2*offset,width,2*offset,
					   0,3*offset,width,3*offset, 
					   0,4*offset,width,4*offset,
					   0,5*offset,width,5*offset,
					   0,6*offset,width,6*offset};
		
//		for (int i = 0; i < 10; i = i+7){
//			// vertical lines
//			pts[i]   = i*offset;
//			pts[i+1] = 0;
//			pts[i+2] = i*offset;
//			pts[i+3] = height;
//			// horizontal lines
//			pts[i+4] = 0;
//			pts[i+5] = i*offset;
//			pts[i+6] = width;
//			pts[i+7] = i*offset;
//		}
		
		canvas.drawLines(pts, pGrid);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float tx = event.getX();
		float ty = event.getY();
		
		
		int touch = event.getAction();
		
		if(tx > 110 && tx < 130 && ty > 44 && ty < 108 && touch == MotionEvent.ACTION_MOVE){
			touched = true;
		}
		
		
		return super.onTouchEvent(event);
	}
}
